#include<stdio.h>
#include<time.h>
#include<vector>
#include<iostream>
#include<windows.h>
using namespace std;
template<class T>
class ConstLengthMemoryPool
{
public:
    T* New()
    {
        T* obj=nullptr;
        if(_freeList)
        {
            void* next=*(void**)_freeList;
            obj=(T*)_freeList;
            _freeList=next;
            return obj;
        }
        else
        {
            if(_remainSize-sizeof(T)<0)
            {
                _remainSize=128*1024;
                _memory=(char*)malloc(_remainSize);
            }
            obj=(T*)_memory;
            size_t ObjSize=sizeof(T)<sizeof(void*)?sizeof(void*):sizeof(T); //如果对象大小小于一个指针的大小就给它一个指针大小的空间，反之就给一个T对象的大小
            _memory+=ObjSize;
            _remainSize-=ObjSize;
        }
        new (obj)T;
        return obj;
    }
    void Delete(T* obj)
    {
        obj->~T();
        *(void**)obj=_freeList;
        _freeList=obj; 

    }
private:
    char* _memory=nullptr;
    size_t _remainSize=0;
    void* _freeList=nullptr;
};
